<template>
  <div class="navbar">
    <div class="wrapper content">
      <NuxtLink class="content-left" to="/">
        <img class="logo" src="../../assets/images/logo.png" alt="" />
        <h1 class="title">OPPO手机商城</h1>
      </NuxtLink>
      <ul class="content-center">
        <template v-for="(item, index) in navbar" :key="index">
          <li :class="{ active: currentIndex === index }">
            <NuxtLink class="link" :to="getPagePath(item)" @click="handleItemClick(index)">
              {{ item.title }}
            </NuxtLink>
          </li>
        </template>
      </ul>
      <app-search class="content-right"></app-search>
    </div>
  </div>
</template>
<script lang="ts" setup>
const props = defineProps({
  navbar: {
    type: Array,
    default: () => [],
  },
})

const currentIndex = ref<number>(0)

const getPagePath = computed(() => {
  return (item: any) => {
    let path = '/'
    if (item.type !== 'oppo') {
      path += item.type // type = onePlus
    }
    return path
  }
})

function handleItemClick(index: number) {
  currentIndex.value = index
}
</script>

<style lang="less" scoped>
.navbar {
  height: 84px;
  z-index: 99;
  border-bottom-color: rgba(0, 0, 0, 0.06);
  border-bottom-style: solid;
  border-bottom-width: 1px;
  /* 吸顶 */
  position: sticky;
  top: 36px;
  z-index: 100;
  background-color: white;
  .content {
    height: 100%;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    .content-left {
      display: inline-block;
      width: 250px;
      height: 50px;
      .logo {
        height: 100%;
      }
      .title {
        text-indent: -1000px;
        height: 0;
        margin: 0;
      }
    }

    .content-center {
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      width: 530px;
      margin-left: 60px;
      .link {
        font-size: 14px;
        color: #000;
        opacity: 0.55;
      }
      .link:hover {
        opacity: 1;
      }
      .active .link {
        font-weight: 600;
        opacity: 1;
      }
    }

    .content-right {
      margin-left: 50px;
    }
  }
}
</style>
